//
//  GoodsViewController.m
//  Leisure
//
//  Created by xalo on 16/6/30.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "GoodsViewController.h"

@interface GoodsViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataSourceArray;
@property (nonatomic,assign)NSInteger index;
@property (nonatomic,assign)NSInteger newsTableViewStarts;
@property (nonatomic,assign)NSInteger newsTableViewLimits;

@end

@implementation GoodsViewController

-(NSMutableArray *)dataSourceArray{
    if (!_dataSourceArray) {
        _dataSourceArray = [NSMutableArray array];
    }
    return _dataSourceArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"良品";
    [self.tableView registerNib:[UINib nibWithNibName:@"GoodsCell" bundle:nil] forCellReuseIdentifier:@"goods_cell"];
    [self loadViewWithStart:0 limit:10];
    self.tableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        [self.dataSourceArray removeAllObjects];
        [self refreshViewWithStart:0 limit:10];
        
    }];
    self.tableView.mj_footer = [MJRefreshBackFooter footerWithRefreshingBlock:^{
        self.newsTableViewStarts += 10;
        [self loadViewWithStart:self.newsTableViewStarts limit:10];
    }];
}

//创建下拉刷新
-(void)refreshViewWithStart:(NSInteger )start limit:(NSInteger )limit{

    //请求数据
    [RequestManager fetchDataWithUrl:kGoodsURL parameter:@{@"start":@(start),@"limit":@(limit)} method:@"GET" completed:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@",dic);
        for (NSDictionary *dict in dic[@"data"][@"list"]) {
            GoodsModel *model = [[GoodsModel alloc]init];
            [model setValuesForKeysWithDictionary:dict];
            [self.dataSourceArray addObject:model];
        }
    } updateUI:^{

        [self.tableView.mj_header endRefreshing];
        [self.tableView reloadData];
        
    }];
}

//创建上拉加载
-(void)loadViewWithStart:(NSInteger)start limit:(NSInteger)limit{
    [RequestManager fetchDataWithUrl:kGoodsURL parameter:@{@"start":@(start),@"limit":@(limit)} method:@"POST" completed:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@",dic);
        for (NSDictionary *dict in dic[@"data"][@"list"]) {
            GoodsModel *model = [[GoodsModel alloc]init];
            [model setValuesForKeysWithDictionary:dict];
            [self.dataSourceArray addObject:model];
        }
    } updateUI:^{
        [self.tableView.mj_footer endRefreshing];
        
        [self.tableView reloadData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    GoodsCell *cell =[tableView dequeueReusableCellWithIdentifier:@"goods_cell" forIndexPath:indexPath];
    [cell setCellWithModel:self.dataSourceArray[indexPath.row]];
    return cell;
    
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    GoodsCell *cell  =[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section] ];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [cell.buyButton addGestureRecognizer:tap];
    
    [cell.buyButton becomeFirstResponder];
     self.index = indexPath.row;

 

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}
-(void)tapAction:(UITapGestureRecognizer *)sender{
    //打开网页
    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:[self.dataSourceArray[self.index] buyurl]]];

}

@end
